package com.itheima.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.UmsAdminMapper;
import com.itheima.pojo.UmsAdmin;
import com.itheima.vo.PageResultVo;
import io.netty.util.internal.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@DubboService
public class UmsAdminImpl implements UmsAdminApi {
    @Autowired
    private UmsAdminMapper mapper;

    @Override
    public UmsAdmin login(String username) {
        UmsAdmin admin = mapper.selectOne(new LambdaQueryWrapper<UmsAdmin>().eq(UmsAdmin::getUsername, username));

        return admin;
    }

    @Override
    public UmsAdmin findById(Long userId) {
        UmsAdmin admin = mapper.selectById(userId);
        return admin;
    }

    @Override
    public PageResultVo list(String keyword, Integer pageNum, Integer pafeSize) {
        LambdaQueryWrapper<UmsAdmin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!StringUtil.isNullOrEmpty(keyword), UmsAdmin::getUsername, keyword);
        Page<UmsAdmin> page = new Page<>(pageNum, pafeSize);
        Page<UmsAdmin> umsAdminPage = mapper.selectPage(page, wrapper);
        PageResultVo resultVo = new PageResultVo<>();
        resultVo.setList(umsAdminPage.getRecords());
        resultVo.setPageNum(pageNum);
        resultVo.setPageSize(pafeSize);
        resultVo.setTotal((int) umsAdminPage.getTotal());
        resultVo.setTotalPages((int) umsAdminPage.getPages());
        return resultVo;
    }

    @Override
    public boolean deleteById(Long userId) {
        UmsAdmin admin = mapper.selectById(userId);
        if (admin.getStatus()==1)return false;
        mapper.deleteById(userId);
        return true;
    }

    @Override
    public boolean register(UmsAdmin admin) {
        UmsAdmin umsAdmin
                = mapper.selectOne(new LambdaQueryWrapper<UmsAdmin>().eq(UmsAdmin::getUsername, admin.getUsername()));
        if (ObjectUtil.isEmpty(umsAdmin)) {
            admin.setPassword(DigestUtil.md5Hex(admin.getPassword()));
            admin.setCreateTime(LocalDateTime.now());
            mapper.insert(admin);
            return true;
        }
        return false;
    }

    @Override
    public List<UmsAdmin> selectAll() {
        List<UmsAdmin> admins = mapper.selectList(new LambdaQueryWrapper<>());
        return admins;
    }

    @Override
    public void updateAdmin(UmsAdmin admin) {
        mapper.updateById(admin);
    }

    @Override
    public void updatePassword(UmsAdmin admin) {
        mapper.update(admin, new LambdaQueryWrapper<UmsAdmin>().eq(UmsAdmin::getUsername, admin.getUsername()));

    }

    @Override
    public void updateStatus(Long id, Integer status) {
        UmsAdmin admin = new UmsAdmin();
        admin.setStatus(status);
        mapper.update(admin, new LambdaQueryWrapper<UmsAdmin>().eq(UmsAdmin::getId, id));

    }
}
